package com.senmo.request;

import com.google.gson.reflect.TypeToken;
import com.senmo.model.BaseResult;
import com.senmo.model.ChannelListResult;
import com.senmo.model.DeviceListResult;
import com.senmo.model.DeviceShareResult;
import com.senmo.model.DeviceShareTimeResult;
import com.senmo.model.LoginResult;
import com.senmo.model.SharedUsersReuslt;

/**
 * author: lx
 * date: 16-1-1
 */
public class RequestFactory {

    public static IRequest login(String mobile, String password) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .put("phoneno", mobile)
                .put("secret", password);
        return new VolleyJsonRequest<>(Api.LOGIN, builder.build(),
                new TypeToken<LoginResult>(){});
    }

    public static IRequest register(String mobile, String password) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .put("phoneno", mobile)
                .put("secret", password);
        return new VolleyJsonRequest<>(Api.REGISTER, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest getDeviceList() {
        JsonParamBuilder builder = JsonParamBuilder.newInstance().putSessionId();
        return new VolleyJsonRequest<>(Api.GET_DEVICE_LIST, builder.build(),
                new TypeToken<DeviceListResult>(){});
    }

    public static IRequest getChannelList(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.GET_CHANNEL_LIST, builder.build(),
                new TypeToken<ChannelListResult>(){});
    }

    public static IRequest bindDevice(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.BIND_DEVICE, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest unbindDevice(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.UNBIND_DEVICE, builder.build(),
                new TypeToken<BaseResult>(){});
    }


    // ==============================================
    // manage device


    public static IRequest getSharedUsers(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.GET_DEVICE_SHARED_USERS, builder.build(),
                new TypeToken<SharedUsersReuslt>(){});
    }

    public static IRequest enableDeviceShare(String device_id, boolean share) {
        int value = share ? DeviceShareResult.SHARE_ENABLED : DeviceShareResult.SHARE_DISABLED;
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id).put("value", value);
        return new VolleyJsonRequest<>(Api.SET_DEVICE_SHARE, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest getIsDeviceShare(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.GET_DEVICE_SHARE, builder.build(),
                new TypeToken<DeviceShareResult>(){});
    }

    public static IRequest setDeviceShareTime(String device_id, String startTime, String endTime) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId()
                .put("device_id", device_id)
                .put("start_time", startTime)
                .put("end_time", endTime);
        return new VolleyJsonRequest<>(Api.SET_DEVICE_SHARE_TIME, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest getDeviceShareTime(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.GET_DEVICE_SHARE_TIME, builder.build(),
                new TypeToken<DeviceShareTimeResult>(){});
    }

    public static IRequest shareDevice(String device_id, String user_phone) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id).put("user_phoneno", user_phone);
        return new VolleyJsonRequest<>(Api.SHARE_DEVICE, builder.build(),
                new TypeToken<DeviceShareTimeResult>(){});
    }

    public static IRequest unShareDevice(String device_id, String... phones) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < phones.length; i ++) {
            sb.append(phones[i]);
            if (i != phones.length - 1) {
                sb.append(",");
            }
        }
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id).put("user_phoneno", sb.toString());
        return new VolleyJsonRequest<>(Api.UN_SHARE_DEVICE, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest acceptShareRequest(String device_id, String user_phone) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id).put("user_phoneno", user_phone);
        return new VolleyJsonRequest<>(Api.ACCEPT_SHARE_REQUEST, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest getShareRequests() {
        JsonParamBuilder builder = JsonParamBuilder.newInstance().putSessionId();
        return new VolleyJsonRequest<>(Api.GET_SHARE_REQUEST_MESSAGES, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest transferDeviceAuth(String device_id, String user_phone) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id).put("user_phoneno", user_phone);
        return new VolleyJsonRequest<>(Api.TRANSFER_DEVICE_AUTH, builder.build(),
                new TypeToken<BaseResult>(){});
    }


    // ==============================================
    // access device

    public static IRequest requestSharedDevice(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.REQUEST_SHARED_DEVICE, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest requestAccessDevice(String device_id) {
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId().put("device_id", device_id);
        return new VolleyJsonRequest<>(Api.REQUEST_ACCESS_DEVICE, builder.build(),
                new TypeToken<BaseResult>(){});
    }

    public static IRequest acceptSharedDevice(String device_id, String managerPhone, boolean accept) {
        int value = accept ? 1 : 2;
        JsonParamBuilder builder = JsonParamBuilder.newInstance()
                .putSessionId()
                .put("device_id", device_id)
                .put("manager_phoneno", managerPhone)
                .put("value", value);
        return new VolleyJsonRequest<>(Api.ACCEPT_SHARED_DEVICE, builder.build(),
                new TypeToken<BaseResult>(){});
    }

}
